using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;


namespace org.synthful.Profiles
{
    /// <summary>
    /// Offered under Apache Public Licence 2.0
    /// synthful.org
    /// blessedgeek [@] synthful.org
    /// </summary>
    static public class AppFolders
    {
        static AppFolders()
        {
            AppName = Process.GetCurrentProcess().ProcessName;
            LaunchFolder = System.Environment.CurrentDirectory;
        }

        static public string ProgramsFolder
        {
            get
            {
                if (folderExists(_Defaults.ProgramsFolder))
                    return _Defaults.ProgramsFolder;

                return
                    System.Environment.GetFolderPath(
                        System.Environment.SpecialFolder.ProgramFiles);
            }

            set { _Defaults.ProgramsFolder = value; }
        }

        static public string AppProgramFolder
        {
            get
            {
                if (folderExists(_Defaults.AppProgramFolder))
                    return _Defaults.AppProgramFolder;

                return
                    ProgramsFolder +'/'+ AppName;
            }

            set { _Defaults.AppProgramFolder = value; }
        }

        static public string CommonDataFolder
        {
            get
            {
                if (folderExists(_Defaults.CommonDataFolder))
                    return _Defaults.CommonDataFolder;

                return
                    System.Environment.GetFolderPath(
                        System.Environment.SpecialFolder.CommonApplicationData);
            }

            set { _Defaults.CommonDataFolder = value; }
        }

        static public string AppDataFolder
        {
            get
            {
                if (folderExists(_Defaults.AppDataFolder))
                    return _Defaults.AppDataFolder;

                return
                    CommonDataFolder +'/'+ AppName;
            }

            set { _Defaults.AppDataFolder = value; }
        }

        static public string UserDataFolder
        {
            get
            {
                return
                    System.Environment.GetFolderPath(
                        System.Environment.SpecialFolder.ApplicationData);
            }
        }

        static public string AppUserDataFolder
        {
            get
            {
                return UserDataFolder +'/'+ AppName;
            }
        }

        static public bool folderExists(string f)
        {
            return
                f != null &&
                f.Length > 0 &&
                Directory.Exists(f);
        }

        static public bool LaunchFolderAsDefault
        {
            get
            {
                return
                    _Defaults.AppProgramFolder == LaunchFolder &&
                    _Defaults.CommonDataFolder == LaunchFolder;
                    ;
            }
            set
            {
                if (value)
                {
                    _Defaults.AppProgramFolder = LaunchFolder;
                    _Defaults.CommonDataFolder = LaunchFolder;
                }
            }
        }

        static public string
            AppName,
            LaunchFolder
            ;

        public static string DefaultSettingsFolder(bool useLaunchFolderAsDefault)
        {
            return
                AppFolders.DefaultDataFolder(useLaunchFolderAsDefault) + '/' +
                AppFolders._Defaults.SettingsSubFolder;
        }

        public static string DefaultDataFolder(bool useLaunchFolderAsDefault)
        {
            AppFolders.LaunchFolderAsDefault = useLaunchFolderAsDefault;

            return
                AppFolders.CommonDataFolder;
        }

        static private Defaults _Defaults = new Defaults("settings");

        public struct Defaults
        {
            public Defaults(string settingsSubFolder)
                :
                this (true, null, null, null, null, settingsSubFolder)
            {
            }

            public Defaults(
                bool launchFolderAsDefault,
                string programsFolder,
                string appProgramFolder,
                string commonDataFolder,
                string appDataFolder,
                string settingsSubFolder
                )
            {
                this.LaunchFolderAsDefault = launchFolderAsDefault;
                this.ProgramsFolder = programsFolder;
                this.AppProgramFolder = appProgramFolder;
                this.CommonDataFolder = commonDataFolder;
                this.AppDataFolder = appDataFolder;
                this.SettingsSubFolder = settingsSubFolder;
            }

            public bool
                LaunchFolderAsDefault;

            public string
                ProgramsFolder,
                AppProgramFolder,
                CommonDataFolder,
                AppDataFolder,
                SettingsSubFolder;
        }
    }

}
